package org.example.studentsystem_back.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.example.studentsystem_back.common.CommonResponse;
import org.example.studentsystem_back.domain.Students;
import org.example.studentsystem_back.domain.Users;
import org.example.studentsystem_back.dto.PageDTO;
import org.example.studentsystem_back.service.UsersService;
import org.example.studentsystem_back.vo.dataCenterVo.UserDataCenterVo.UserDataCenterVo;
import org.example.studentsystem_back.vo.dataCenterVo.UserDataCenterVo.UserRoleProVo;
import org.example.studentsystem_back.vo.notificitionsVo.NoVo;
import org.example.studentsystem_back.vo.userMangeVo.UserLoginVo;
import org.example.studentsystem_back.vo.userMangeVo.UserStudentVo;
import org.example.studentsystem_back.vo.userMangeVo.UserVo;
import org.example.studentsystem_back.vo.userManageVo.PageUserVo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@Valid
public class UsersController {
    @Resource
    private UsersService usersService;

    //用户注册
    @PostMapping("/users/register")
    public CommonResponse<Users> register(@Validated @RequestBody Users users) throws Exception {
        return usersService.register(users);
    }

    //用户登录
    @PostMapping("/users/login")
    public CommonResponse<UserLoginVo> login(@Validated @RequestBody Users users) throws Exception {
        return usersService.login(users);
    }

    //查询所有用户
    @GetMapping("/users")
    public CommonResponse<List<UserVo>> getAllUsers() throws Exception {
        return usersService.getAllUsers();
    }

    //根据userid获取用户信息
    @GetMapping("/users/{id}")
    public CommonResponse<UserVo> getUserByUserid(@PathVariable("id") String userid) throws Exception{
        return usersService.getUserByUserid(userid);
    }

    //获取当前用户
    @GetMapping("/users/current")
    public CommonResponse<UserVo> getCurrentUser(HttpServletRequest request){
        return usersService.getCurrentUser(request);
    }

    //用户查询分页
    @PostMapping("/users/page")
    public CommonResponse<PageUserVo> getUsersByPage(@RequestBody PageDTO pageDTO) throws Exception{
        return usersService.getUsersByPage(pageDTO);
    }

    //获取平台用户角色比例
    @GetMapping("/users/dataCenter")
    public CommonResponse<UserDataCenterVo> getUserDataCenter(){
        return usersService.getAllUserDataCenter();
    }

    @PutMapping("/users/{id}")
    public CommonResponse<UserVo> updateUserInfo(@PathVariable("id") String userid,@RequestBody String json){
        return usersService.updateUserInfo(json);
    }
}
